<?php
use com\gdl\kq\interfaces\AppAttendanceServiceIf;
use com\gdl\kq\dataobject\attendancegroup\AttendanceGroupDTO;
use com\gdl\common\dataobject\common\ResultDO;
date_default_timezone_set("PRC");
/**
 *  考勤
 *  @author gdl
 */
class api_AppAttendanceService extends api_Abstract implements AppAttendanceServiceIf
{

    #需要用到的model
    public $groupModel;#考勤组
    public $groupUserModel; #人员所属考勤组
    public $approvalModel; #审批层级表
    public $outGoModel; #外出申请记录表
    public $cardModel; #补卡申请记录表
    public $leaveModel; #请假申请记录表
    public $overtimeModel; #加班申请记录表
    public $attRecordModel; #考勤记录表
    public $violationModel; #人脸记录表
    public $groupmemberrecordModel; #考勤组人员档案表
    public $setholidaysModel; #节假日定义方案表
    public $apppublicarea; #获取公共考勤地点

    public function __construct()
    {
        parent::__construct();
        $this->groupModel = new model_attendance_attendancegroup();

        $this->groupUserModel = new model_attendance_attendancegroupuser();
        $this->approvalModel = new model_attendance_approvalrecord();
        $this->outGoModel = new model_attendance_appoutgorecord();
        $this->cardModel = new model_attendance_approvalprocesscardreplacementrecord();
        $this->leaveModel = new model_attendance_approvalprocessleaverecord();
        $this->overtimeModel = new model_attendance_overtimerecord();
        $this->attRecordModel = new model_attendance_attendancerecord();
        $this->violationModel = new model_attendance_violation();
        $this->groupmemberrecordModel = new model_attendance_groupmemberrecord();
        $this->setholidaysModel = new model_attendance_setholidays();
        $this->apppublicarea = new model_attendance_apppublicarea();
    }

    //统计接口APP
    public function statisticsAppGet(AttendanceGroupDTO $attendancegroup)
    {
        $data = $attendancegroup->data ? gdl_lib_BaseUtils::getStr($attendancegroup->data) : "";
        #接受参数
        $date = $data['date'] ? $data['date'] : 0;
        $userId = $data['userId'] ? $data['userId'] : 0;
        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        if (empty($userId)) {
            $result->message = "请选择用户";
            return $result;
        }
        if (empty($date)) {
            $result->message = "请选择日期";
            return $result;
        }
        try {
            $recordings = [
                'missCard' => ['num' => 0, 'data' => []],
                //缺卡
                'late' => ['num' => 0, 'data' => []],
                //迟到
                'leavEarly' => ['num' => 0, 'data' => []],
                //早退
                'completion' => ['num' => 0, 'data' => []],
                //旷工
                'onTime' => ['num' => 0, 'data' => []],
                //准时
                'field' => ['num' => 0, 'data' => []],
                //外勤
                'leave' => ['num' => 0, 'data' => []],
                //请假
                'supCard' => ['num' => 0, 'data' => []],
                //补卡申请
                'abnormal' => ['num' => 0, 'data' => []],
                //异常
            ];
            //
            //	$result->message = json_encode($data);
            //       return $result;
            $tNum = date("t", strtotime($date));
            //选择日期的天数
            $start_time = strtotime(date('Y-m-01', strtotime($date)));
            //获取本月第一天时间戳
            $array = array();
            for ($i = 0; $i < $tNum; $i++) {
                $array[] = date('Y-m-d', $start_time + $i * 86400);
                //每隔一天赋值给数组
            }
            if ($array) {
                $array = implode("','", $array);
                $array = "'" . $array . "'";
            } else {
                $array = 1;
            }

            $condition = "memberId = {$userId} and sureDate in ({$array}) and  isRest=1 and notClockin=1 and offDutyOut=IF(clockinType=2,1,offDutyOut)";
            $field = "sum(if((no=2 && notClockin=1 && isRest=1 && leaveIn=1 ),1,0)) as missCard,sum(if(abnormal=1 && notClockin=1 && isRest=1 && leaveIn=1 ,1,0)) as late,sum(if(abnormal=2 && notClockin=1 && isRest=1 && leaveIn=1 ,1,0)) as leavEarly,sum(if(absenteeism>=1  && notClockin=1 && isRest=1 && leaveIn=1 ,1,0)) as completion";
            $lists = $this->attRecordModel->selectOne($condition, $field);
            
            foreach ($recordings as $key => $value) {
                $onTimeList = [];
                if ($key == "onTime") {
                    //
                    $onTimeList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and leaveIn!=2  and notClockin=1 and isRest=1  and offDutyOut=IF(clockinType=2,1,offDutyOut)
                    and ((absenteeism=0 and abnormal=0 and no=1) or (isGo=2 and no=1) or (no=3 && absenteeism=0 and abnormal=0))", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                    // absenteeism=0 and abnormal=0 and (no=1 || (no=3 && absenteeism=0 and abnormal=0))  or leaveIn=1  or isGo=2

                    $recordings['onTime']['num'] = !empty($onTimeList) ? count($onTimeList) : 0;
                }elseif ($key == "field") {
                    $onTimeList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and isGo=2", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                    // absenteeism=0 and abnormal=0 and (no=1 || (no=3 && absenteeism=0 and abnormal=0))  or leaveIn=1  or isGo=2

                    $recordings['field']['num'] = !empty($onTimeList) ? count($onTimeList) : 0;
                }elseif ($key == "leave") {
                    $onTimeList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and leaveIn=2", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                    // absenteeism=0 and abnormal=0 and (no=1 || (no=3 && absenteeism=0 and abnormal=0))  or leaveIn=1  or isGo=2

                    $recordings['leave']['num'] = !empty($onTimeList) ? count($onTimeList) : 0;
                }elseif ($key == "supCard") {
                    $onTimeList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and no=3", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                    // absenteeism=0 and abnormal=0 and (no=1 || (no=3 && absenteeism=0 and abnormal=0))  or leaveIn=1  or isGo=2

                    $recordings['supCard']['num'] = !empty($onTimeList) ? count($onTimeList) : 0;
                }elseif ($key == "abnormal") {
                    $onTimeList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and (abnormalIdentityIs=2 or abnormalCoordinateIs=2)", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                    // absenteeism=0 and abnormal=0 and (no=1 || (no=3 && absenteeism=0 and abnormal=0))  or leaveIn=1  or isGo=2

                    $recordings['abnormal']['num'] = !empty($onTimeList) ? count($onTimeList) : 0;
                } else {
                    $recordings[$key]['num'] = !empty($lists[$key])?$lists[$key]:0;
                }
                $chiNum = array('七', '一', '二', '三', '四', '五', '六');
                $num = array(1 => "第一次", 2 => "第二次", 3 => "第三次");
                $baseTypes = array("上下班", "上班", "下班");
                $leaveTypes = array("事假", "年假", "事假", "调休", "产假", "婚假", "丧假","护理假", "病假");
                //获取记录列表
                switch ($key) {
                    case 'missCard':
                        //缺卡
                        if ($recordings[$key] != 0) {
                            $missCardList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and no=2 and notClockin=1 and isRest=1 and leaveIn=1 ", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                            foreach ($missCardList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                            }
                            $recordings['missCard']['data'] = $missCardList ? $missCardList : [];
                        }
                        break;
                    case 'late':
                        //迟到
                        if ($recordings[$key] != 0) {
                            $lateList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and abnormal=1", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                            foreach ($lateList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                                $lateTime = $mC['clockinTime'] - strtotime($mC['sureDate'] . " " . $mC['sureTime']);
                                $mC['mark'] = "上班迟到" . gmdate("H小时i分钟", $lateTime);
                            }
                            $recordings['late']['data'] = $lateList ? $lateList : [];
                        }
                        break;
                    case 'leavEarly':
                        //早退
                        if ($recordings[$key] != 0) {
                            $leavEarlyList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and abnormal=2", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                            foreach ($leavEarlyList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                                $leavEarlyTime = strtotime($mC['sureDate'] . " " . $mC['sureTime']) - $mC['clockinTime'];
                                $mC['mark'] = "下班早退" . gmdate("H小时i分钟", $leavEarlyTime);
                            }
                            $recordings['leavEarly']['data'] = $leavEarlyList ? $leavEarlyList : [];
                        }
                        break;
                    case 'completion':
                        //旷工
                        if ($recordings[$key] != 0) {
                            $completionList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and absenteeism>=1", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                            foreach ($completionList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                            }
                            $recordings['completion']['data'] = $completionList ? $completionList : [];
                        }
                        break;
                    case 'onTime':
                        //准时
                        foreach ($onTimeList as &$mC) {
                            $week = date('w', strtotime($mC['sureDate']));
                            $mC['week'] = "星期" . $chiNum[$week];
                        }
                        $recordings['onTime']['data'] = !empty($onTimeList[0]['id']) ? $onTimeList : [];
                        break;
                    case 'field':
                        //外勤
                        if ($recordings[$key] != 0) {
                            $fieldList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and isGo=2", "id,sureDate,sureTime,clockinTime,clockinType")->items;
                            foreach ($fieldList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                            }
                            $recordings['field']['data'] = $fieldList ? $fieldList : [];
                        }
                        break;
                    case 'leave':
                        //2018-08-27 二改
//                        $beginThismonth = strtotime(date('Y-m-01', strtotime($date)));
//                        $endThismonth = strtotime(date('Y-m-d', strtotime(date('Y-m-01', strtotime($date))." +1 month -1 day")));
//                        $leaveLists =  $this->leaveModel->select(
//                            "memberId = {$userId} and startTime>$beginThismonth and endTime<$endThismonth and status=2",
//                            "id leaverecordId,startTime,endTime,createTime,leaveType"
//                        )->items;
//                        $leaveList = [];
//                         foreach ($leaveLists as $mC) {
//                             //                                $mC['week'] = "星期" . $chiNum[$week];
//                             $id = $this->attRecordModel->selectOne(" leaveInId={$mC['leaverecordId']} and isDelete=0 ", "id");
//                             $mC['week'] = "请假开始时间:".date('Y-m-d H:i',$mC['startTime'])."--结束时间:".date('Y-m-d H:i',$mC['endTime']);
//                             $mC['leaveType'] = $leaveTypes[$mC['leaveType']];
//                             $mC['leaveStartTime'] = $mC['startTime'];
//                             $mC['leaveEndTime'] = $mC['endTime'];
//                             $mC['sureDate'] = date("Y-m-d",$mC['createTime']);
//                             $mC['id'] = $id?$id['id']:0;
//                             $leaveList[] = $mC;
//                         }
//                        $recordings['leave']['num'] = $leaveList ? count($leaveList) :0;
//                        $recordings['leave']['data'] = $leaveList ? $leaveList : [];
                        //请假
                        if ($recordings[$key] != 0) {
                            $leaveList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and leaveIn=2", "id,sureDate,sureTime,clockinTime,leaveInId,clockinType")->items;
                            foreach ($leaveList as &$mC) {
                                $leaverecordInfo = $this->leaveModel->selectOne(" id = {$mC['leaveInId']}", " id leaverecordId,startTime,endTime,leaveType");
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                                $mC['leaveType'] = $leaverecordInfo ? $leaveTypes[$leaverecordInfo['leaveType']] : "事假";
                                $mC['leaveStartTime'] = $leaverecordInfo['startTime'];
                                $mC['leaveEndTime'] = $leaverecordInfo['endTime'];
                            }
                            $recordings['leave']['data'] = $leaveList ? $leaveList : [];
                        }
                        break;
                    case 'supCard':
                        //补卡申请
                        if ($recordings[$key] != 0) {
                            $supCardList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and no=3", "id,sureDate,sureTime,clockinTime,noId,clockinType")->items;
                            foreach ($supCardList as &$mC) {
                                $replacementrecordInfo = $this->cardModel->selectOne(" id = {$mC['noId']}", " id replaceMentRecordId,replacementTime,num,baseType");
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                                $mC['replacementTime'] = $replacementrecordInfo['replacementTime'] . "(" . $num[$replacementrecordInfo['num']] . $baseTypes[$replacementrecordInfo['baseType']] . ")";
                                //补卡班次
                            }
                            $recordings['supCard']['data'] = $supCardList ? $supCardList : [];
                        }
                        break;
                    case 'abnormal':
                        //异常记录
                        if ($recordings[$key] != 0) {
                            $abnormalList = $this->attRecordModel->select(" memberId = {$userId} and sureDate in ({$array}) and (abnormalIdentityIs=2 or abnormalCoordinateIs=2)", "id,sureDate,sureTime,abnormalIdentityIs,abnormalCoordinateIs,vioLationId,clockinCoordinate,clockinType")->items;
                            foreach ($abnormalList as &$mC) {
                                $week = date('w', strtotime($mC['sureDate']));
                                $mC['week'] = "星期" . $chiNum[$week];
                                if ($mC['abnormalIdentityIs'] == 2) {
                                    $mark[] = "身份异常";
                                }
                                if ($mC['abnormalCoordinateIs'] == 2) {
                                    $mC['clockinCoordinate'] = unserialize($mC['clockinCoordinate']);
                                    $mark[] = "地点异常";
                                }
                                $mC['abnormalType'] = implode(',', $mark);
                                $mark = [];
                            }
                            $recordings['abnormal']['data'] = $abnormalList ? $abnormalList : [];
                        }
                        break;
                }
            }
            $result->data = [[json_encode($recordings)]];
            $result->message = '成功';
            $result->code = 200;
        } catch (Exception $e) {
            $result->message = '异常';
        }
        return $result;
    }
    //获取人员每天的考勤租详细接口（包括选择日期是否为外勤）
    public function sbqAppGetUserDaysAttendanceGroupInfo(AttendanceGroupDTO $attendancegroup)
    {
        $data = $attendancegroup->data ? gdl_lib_BaseUtils::getStr($attendancegroup->data) : "";
        #接受参数
        $platformId = $data['platformId'] ? $data['platformId'] : 0;
        $date = $data['date'] ? $data['date'] : 0;
        $userId = $data['userId'] ? $data['userId'] : 0;
        $unitId = $data['unitId'] ? $data['unitId'] : 0;
        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
        #验证
        if (empty($userId)) {
            $result->message = "请选择用户";
            return $result;
        }
        if (empty($date)) {
            $result->message = "请选择日期";
            return $result;
        }
        try {
            $t = time();
            $today = date('Y-m-d', $t);
            $istoday = $today == $date ? 1 : 2;
            //1 今天 2 不是今天
            $undate = strtotime($date);
            //选择日期的时间戳
            $row = $this->getClockinRecordInfo($t, $undate, $userId, $platformId);
            if ($row['code'] == '200') {
                $status = $row['status'];
                ////status 1 有考勤记录 2今天休息  3暂无排班
                $result->data = [['data' => json_encode($row['data']), 'status' => $status]];
                $result->message = $row['msg'];
                $result->code = 200;
            } else {
                $result->message = $row['msg'];
            }
        } catch (Exception $e) {
            $result->message = $e->getMessage();
        }
        return $result;
    }
    //
    public function sbqAppFaceRecognition(AttendanceGroupDTO $attendancegroup)
    {
        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
        return $result;
    }
    //添加身份验证记录
    public function addViolation(AttendanceGroupDTO $attendancegroup)
    {
        $resultDO = new ResultDO();
        $data = $attendancegroup->data;
        try {
            $resultDO->code = 200;
            $resultDO->success = true;
            if (!$data['memberId']) {
                $resultDO->message = "信息不全,添加失败，请刷新再试!";
                return $resultDO;
            }
            $id = $this->violationModel->insert($data);
            ///////////////
            if ($id) {
                $resultDO->message = '添加成功';
                $resultDO->message = $id;
                $resultDO->code = 200;
            } else {
                $resultDO->code = 400;
                $resultDO->message = 0;
            }
            return $resultDO;
        } catch (Exception $e) {
            $resultDO->success = false;
            $resultDO->code = 400;
        }
        return $resultDO;
    }
    //修改用户考勤记录的备注
    public function editAppUserRecordRemark(AttendanceGroupDTO $attendancegroup)
    {
        $resultDO = new ResultDO();
        $data = $attendancegroup->data;
        try {

            $resultDO->code = 400;
            $resultDO->success = true;
            if (!$data['id']) {
                $resultDO->message = "信息不全,添加失败，请刷新再试!";
                return $resultDO;
            }
            $condition = " id = {$data['id']} and memberId={$data['memberId']}";
            $row = ['remark' => $data['remark']];
            $this->attRecordModel->update($condition, $row);
            ///////////////
            if ($data['id']) {
                $resultDO->message = '修改成功';
                $resultDO->message = $data['id'];
                $resultDO->code = 200;
            } else {
                $resultDO->code = 400;
                $resultDO->message = 0;
            }
            return $resultDO;
        } catch (Exception $e) {
            $resultDO->success = false;
            $resultDO->code = 400;
        }
        return $resultDO;
    }

    //签到打卡接口
    public function sbqAppDoSignIn(AttendanceGroupDTO $attendancegroup)
    {
        $resultDO = new ResultDO();
        $data = $attendancegroup->data;
        try {
            $resultDO->code = 400;
            $resultDO->success = true;
            if (!$data['memberId']) {
                $resultDO->message = "信息不全,添加失败，请刷新再试!";
                return $resultDO;
            }
            //判断用户是否存在考勤组中
            $attendancegroupJoin = ['atdc_attendance_group_user as aagu' => 'atdc_attendance_group.id=aagu.agId'];
            $attendancegroupRow = $this->groupModel->selectOne(" aagu.memberId={$data['memberId']}", "atdc_attendance_group.*", "", "", $attendancegroupJoin);
            $date = date('Y-m-d', $data['time']);
            if (!$attendancegroupRow) {
                $resultDO->message = "用户不存在考勤组!";
                return $resultDO;
            } else {
                $time = $data['time'];
                //打卡时间戳
                $platformId = $data['platformId'];
                //
                $memberId = $data['memberId'];
                //会员ID
                $isRest = $data['isRest'];
                //是否是休息日打卡
                $sureTime = $data['sureTime'];
                //正常打卡时间点 ps:09:00:00
                $isGo = $data['isGo'];
                //1正常考勤 2外勤打卡
                $remark = $data['remark'];
                //备注
                $clockinNum = $data['clockinNum'];
                //1第一次打卡  2第二次打卡  3第三次打卡
                $clockinType = $data['clockinType'];
                //1上班打卡 2下班打卡
                //打卡地点 ['title'=>'重庆九龙坡智博中心','deviation'=>'500','coordinate'=>['lng'=>'106.459223','lat'=>'29.509867']]
                $clockinCoordinate = unserialize($data['clockinCoordinate']);
                $coordinateSure = unserialize($data['coordinateSure']);
                $abnormalCoordinateIs = $data['abnormalCoordinateIs'];
                $vioLationId = $data['vioLationId'];
                $abnormalIdentityIs = $data['abnormalIdentityIs'];
                $photo = unserialize($data['photo']);
                if ($photo) {
                    $photo = implode(',', $photo);
                } else {
                    $photo = '';
                }
				$platformId = $platformId?$platformId:$attendancegroupRow['platformId'];
				
                $agId = $attendancegroupRow['id'];
                //考勤组ID
                $agName = $attendancegroupRow['title'];
                //考勤组名称
                //                $goDaysNum     = unserialize($attendancegroupRow['goDaysNum']);//一天上班次数设置及其时间
                $offDutyOut = $attendancegroupRow['offDutyOut'];
                //下班是否需要打卡 1打卡 2不打卡
                $faceStatus = $attendancegroupRow['faceStatus'];
                //下班是否需要打卡 1打卡 2不打卡
                $unusualStatus = unserialize($attendancegroupRow['unusualStatus']);
                //异常定义 序列号数组
                $no = 1;
                //1正常打卡 2缺卡 3补卡 4请假
                $noId = 0;
                //当no字段为3时 是补卡记录ID 当no字段为4时 是请假记录ID
            }
            //查询当前时间和班次 是否已经正常打卡
            $y = $this->attRecordModel->selectOne(" sureDate='{$date}' and clockinNum = {$clockinNum} and clockinType = {$clockinType} and no =1 and memberId={$memberId}");
            if ($y) {
                $resultDO->message = "已打卡，请勿重复打卡!";
                return $resultDO;
            }
            //判断人员是否是无需打卡人员
            $attendancegroupuser = $this->groupUserModel->selectOne(" agId={$agId} and memberId={$memberId} and type=2 and status=0");
            $notClockin = 1;
            //1需要打卡人员打卡  2无需打卡人员打卡
            if ($attendancegroupuser) {
                $notClockin = 2;
            }
            //判断用户是否请假 $sureTime $date
            //正常打卡时间戳
            $strtotimeSure = strtotime($date . " " . $sureTime);

            $rrCondition = " memberId = {$memberId} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
            $rrInfo = $this->leaveModel->selectOne($rrCondition);
            if ($rrInfo) {
                $leaveIn = 2;
                $leaveInId = $rrInfo['id'];
            } else {
                $leaveIn = 1;
                $leaveInId = 0;
            }
            $absenteeism = 0;
            //0没有旷工 1旷工半天  2旷工1天
            $abnormal = 0;
            //1迟到 2早退
            //判断打卡时间有无旷工 迟到 sureTime(正常打卡时间点 ps:09:00:00) $time(打卡时间)
            $nowTime = strtotime($date . " " . $sureTime);
            //记录正常时间点的时间戳

            //是否存在外勤
            $isGoRow = $this->appUserIsOutGo($memberId, $platformId, $sureTime, $date);
            if($isGoRow){
                $isGo = 2;
            }
            if ($isRest == 1 && $notClockin == 1 && $leaveIn==1 && $isGo==1) {
                //休息日不记入异常 无需打卡人员不计入异常 请假不计入异常 外勤不计入异常
                if ($clockinType == 1) {
                    //1上班打卡 2下班打卡
                    $beLate = $unusualStatus['beLate'];
                    $beLateOne = $beLate[0] * 60;
                    $beLateTwo = $beLate[1] * 60;
                    if ($time > $nowTime) {
                        $abnormal = 1;
                        $min = $time - $nowTime;
                        if ($min >= $beLateOne && $min < $beLateTwo && $beLate[0] != 0) {
                            $abnormal = 0;
                            $absenteeism = 1;
                        } elseif ($min >= $beLateTwo && $beLate[1] != 0) {
                            $abnormal = 0;
                            $absenteeism = 2;
                        }
                    }
                } elseif ($clockinType == 2 && $offDutyOut != 2) {
                    //18:00:00//是否需要下班卡 （1开启后，下班不打卡也不会记作异常）
                    $leaveEarly = $unusualStatus['leaveEarly'];
                    $leaveEarlyOne = $leaveEarly[0] * 60;
                    $leaveEarlyTwo = $leaveEarly[1] * 60;
                    if ($time < $nowTime) {
                        //早退
                        $abnormal = 2;
                        $min = $nowTime - $time;
                        if ($min >= $leaveEarlyOne && $min < $leaveEarlyTwo && $leaveEarly[0] != 0) {
                            $abnormal = 0;
                            $absenteeism = 1;
                        } elseif ($min >= $leaveEarlyTwo && $leaveEarly[1] != 0) {
                            $abnormal = 0;
                            $absenteeism = 2;
                        }
                    }
                }
            }
            $dataIns = [
                'memberId' => $memberId,
                'agId' => $agId,
                'agName' => $agName,
                'notClockin' => $notClockin,
                'platformId' => $platformId,
                'isGo' => $isGo,
                'clockinType' => $clockinType,
                'no' => $no,
                'noId' => $noId,
                'clockinNum' => $clockinNum,
                'clockinTime' => $time,
                'clockinDate' => $date,
                'sureTime' => $sureTime,
                'leaveIn' => $leaveIn,
                'leaveInId' => $leaveInId,
                'isRest' => $isRest,
                'offDutyOut' => $offDutyOut,
                'sureDate' => $date,
                'faceStatus' => $faceStatus,
                'vioLationId' => $vioLationId,
                'abnormalIdentityIs' => $abnormalIdentityIs,
                'abnormalCoordinateIs' => $abnormalCoordinateIs,
                'clockinCoordinate' => serialize($clockinCoordinate),
                'coordinate' => serialize($coordinateSure),
                'remark' => $remark,
                //                'unusualRemark'=>serialize($unusualStatus),
                'photo' => $photo,
                'abnormal' => $abnormal,
                'absenteeism' => $absenteeism,
            ];
            $id = $this->attRecordModel->insert($dataIns);
            ///////////////
            if ($id) {
                $resultDO->message = '打卡成功';
                //                $resultDO->data = [[$id]];
                $resultDO->code = 200;
            } else {
                $resultDO->code = 400;
                $resultDO->message = "打卡失败";
            }
            return $resultDO;
        } catch (Exception $e) {
            $resultDO->success = false;
            $resultDO->code = 400;
        }
        return $resultDO;
    }

    //查看打卡记录详细接口
    public function sbqAppSignInInfo(AttendanceGroupDTO $attendancegroup)
    {
        $result = new ResultDO();
        $result->success = true;
        $result->code = 400;
        $result->data = [];
        return $result;
    }
    private function getHi($second)
    {
        $second = $second % (3600 * 24);
        $hour = floor($second / 3600);
        $second = $second % 3600;
        $minute = floor($second / 60);
        // 不用管怎么实现的，能用就ok
        return $hour . ':' . $minute;
    }
    // 获取选择日期的打卡信息
    /**
     * @param $t @@当天的时间戳
     * @param $undate @@选择日期的时间戳
     * @param $userId @@用户ID
     * @param $platformId @@平台ID
     */
    private function getClockinRecordInfo($t, $undate, $userId, $platformId)
    {
        $return = ['code' => 200, 'data' => [], 'msg' => "成功", 'status' => 1];
        //status 1 有考勤记录 2今天休息  3暂无排班 4无需打卡 5无打卡记录
        $date = date('Y-m-d', $undate);
        //判断选择日期是否大于当天
        $startTime = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
        //获取当天的0点时间戳
        $endTime = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
        //获取当天的下午12点时间戳
        $globals = new service_globals();

        
        //节假日定义方案表

        //补卡记录Model


        $recordCondition = " atdc_attendance_record.memberId = {$userId} and sureDate='{$date}' ";
        $leftJoin = ['atdc_violation as av' => 'atdc_attendance_record.vioLationId=av.id'];
        $recordInfos = $this->attRecordModel->select($recordCondition, 'atdc_attendance_record.*', '', '', $leftJoin)->items;
        #记录END
        //获取用户考勤组 START
        $groupuserWhere = "atdc_attendance_group_user.status=0 and atdc_attendance_group_user.memberId={$userId} and aag.isdelete=0 ";
        $leftJoin = array('atdc_attendance_group as aag' => 'atdc_attendance_group_user.agId=aag.id', 'atdc_setholidays as ass' => 'ass.id=aag.setholidaysId');
        $groupFiled = "atdc_attendance_group_user.id as groupUserId,atdc_attendance_group_user.agId,aag.id as agId,aag.setholidaysId,aag.goDaysNum,aag.unusualStatus,aag.title as agName,aag.offDutyOut,aag.coordinate,aag.faceStatus,aag.trackLocation,ass.workday,ass.specialWorkday,ass.holiday";
        $groupuserRow = $this->groupUserModel->select($groupuserWhere, $groupFiled, '', '', $leftJoin)->items;

        //获取用户考勤组 END
        if ($undate < $startTime) {
			$dTime = $undate+86399;
            //历史考勤 查询记录 没有记录则显示补卡
            //查询用户当天是否需要打卡 考勤组人员档案表 根据档案表查询历史配置
            $groupmemberrecordInfo = $this->groupmemberrecordModel->selectOne("FIND_IN_SET('{$userId}',memberIds) and addTime<={$dTime} order by id  desc");

            if (!$groupmemberrecordInfo) {
                //当天没有班次添加
                $return['data'] = [];
                $return['status'] = 3;
                $return['msg'] = "暂无排班";
            }
            else {
                if ($groupmemberrecordInfo) {
                    $agId = $groupmemberrecordInfo['agId'];
                    $workdayKey = unserialize($groupmemberrecordInfo['workday']);
                    $holidayKey = $this->arrayDateToKey(unserialize($groupmemberrecordInfo['holiday']));
                    //特殊工作日定义 [2018-02-11?@(春节(补))','2018-02-11?@(清明节(补))','2018-02-11?@(春节(补))']
                    $specialWorkdayKey = $this->arrayDateToKey(unserialize($groupmemberrecordInfo['specialWorkday']));
                    $goDaysNum = unserialize($groupmemberrecordInfo['goDaysNum']);
                    $offDutyOut = $groupmemberrecordInfo['offDutyOut'];
                    $faceStatus = $groupmemberrecordInfo['faceStatus'];
                    //一天上班次数设置及其时间

                } else {
                    $agId = $groupuserRow[0]['agId'];
                    $workdayKey = $groupuserRow[0]['workday'];
                    $holidayKey = $this->arrayDateToKey(unserialize($groupuserRow[0]['holiday']));
                    $specialWorkdayKey = $this->arrayDateToKey(unserialize($groupuserRow[0]['specialWorkday']));
                    $goDaysNum = unserialize($groupuserRow[0]['goDaysNum']);
                    $offDutyOut = $groupuserRow[0]['offDutyOut'];
                    $faceStatus = $groupuserRow[0]['faceStatus'];
                    //一天上班次数设置及其时间

                }
                //获取日期是否是休息日 bool false是节假日  true不是节假日
                $isHolidaysAndFestivals = $globals->getDateIsHolidaysAndFestivals($date, $workdayKey, $holidayKey, $specialWorkdayKey);
                $isRest = 1;
                //1不是休息日 2休息日
                if (!$isHolidaysAndFestivals) {
                    $return['status'] = 2;
                    //是休息日 2018 07 18 统计休息日打卡
                    $isRest = 2;
                } //
                //                if ($isHolidaysAndFestivals){//True 不是节假日 需要打卡
                $num = $goDaysNum ? count($goDaysNum) : 1;
                $isData = [[], [], [], [], [], []];
                switch ($num) {
                    case 1:
                        unset($isData[5]);
                        unset($isData[4]);
                        unset($isData[3]);
                        unset($isData[2]);
                        break;
                    case 2:
                        unset($isData[5]);
                        unset($isData[4]);
                        break;
                }
                //补充缺卡记录 暂时废弃 采用脚本t统一补卡
                //$this->insertLackCard($recordInfos,$clockinNums,$agId,$userId);
                //查找用户选择的日期是否补过卡
                //查看当前班次是否有补卡记录‘’
                //判断人员是否是无需打卡人员
                $attendancegroupuser = $this->groupUserModel->selectOne(" agId={$agId} and memberId={$userId} and type=2 and status=0");
                $notClockin = 1;
                //1需要打卡人员打卡  2无需打卡人员打卡
                if ($attendancegroupuser) {
                    $notClockin = 2;
                }

                foreach ($recordInfos as $key => $value) {
                    $aa['Id'] = $value['id'];
                    //
                    $aa['lackCard'] = 1;
                    //1 正常记录  2 补卡记录 3缺卡记录 4请假记录
                    $noId = $value['noId'];
                    $aa['noId'] = $noId;
                    $aa['notClockin'] = $notClockin;
                    $aa['offDutyOut'] = $offDutyOut;
                    $aa['isRest'] = $isRest;
                    $aa['lackCardData'] = [];
                    #基础 start
                    $aa['sure'] = 1;
                    //1有记录 2没有记录
                    $aa['memberId'] = $userId;
                    $aa['agId'] = $value['agId'];
                    $aa['faceStatus'] = $value['faceStatus'];
                    $aa['leaveIn'] = $value['leaveIn'];
                    //1正常记录 2请假记录
                    $aa['leaveInId'] = $value['leaveInId'];

                    $aa['overtimeIs'] = $value['overtimeIs'];
                    //1正常记录 2加班记录
                    $aa['overtimeId'] = $value['overtimeId'];
                    //请假ID 默认为0
                    $aa['clockinType'] = $value['clockinType'];
                    $aa['abnormalCoordinateIs'] = $value['abnormalCoordinateIs'];
                    $aa['isGo'] = $value['isGo'];
                    $aa['no'] = $value['no'];
                    $aa['clockinNum'] = $value['clockinNum'];
                    $aa['remark'] = $value['remark'] ? $value['remark'] : '';
                    $aa['abnormalIdentityIs'] = $value['abnormalIdentityIs'];
                    if ($value['abnormalIdentityIs'] != 3) {
                        $aa['abnormalIdentityIsTitle'] = $aa['abnormalIdentityIs'] == 2 ? "未通过" : "已通过";
                    } else {
                        $aa['abnormalIdentityIsTitle'] = "未审核";
                    }
                    $aa['unusualRemark'] = empty($value['unusualRemark']) ? "正常" : $value['unusualRemark'];
                    $aa['clockinTime'] = $value['clockinTime'];
                    $aa['photo'] = empty($value['photo']) ? [] : explode(',', $value['photo']);
                    $aa['timeClockinHi'] = $value['clockinTime'] ? date('H:i', $value['clockinTime']) : '';
                    $clockinCoordinate = unserialize($value['clockinCoordinate']);
                    $clockinCoordinate['title'] = $clockinCoordinate ? $globals->format_html($clockinCoordinate['title']) : "";
                    $aa['clockinCoordinate'] = $clockinCoordinate;
                    $aa['clockinCoordinateName'] = $clockinCoordinate ? $clockinCoordinate['title'] : '未知';
                    $aa['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $aa['sureTime'] = $value['sureTime'];
                    //1迟到 2早退
                    $aa['absenteeism'] = $value['absenteeism'];
                    //0没有旷工 1旷工半天  2旷工1天
                    #基础 end
                    if ($value['abnormal'] == 1) {
                        //1 迟到 2 早退
                        $aa['unusualRemark'] = str_replace('正常', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "迟到";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
                    if ($value['abnormal'] == 2) {
                        //1 迟到 2 早退
                        $aa['unusualRemark'] = str_replace('正常', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "早退";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
                    if ($value['absenteeism'] >= 1) {
                        $aa['unusualRemark'] = str_replace('早退', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('迟到', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "旷工";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
					$ssCondition = "agId={$value['agId']} and memberId = {$value['memberId']} and replacementTime='{$value['sureDate']}' and num={$value['clockinNum']} and baseType={$value['clockinType']} ";
                    $ssInfo = $this->cardModel->selectOne($ssCondition);
					if ($ssInfo && $value['no']!=1){
						$no = $ssInfo['status']==2?3:2;
						$this->attRecordModel->update(" id ={$value['id']}",['no'=>$no,'noId'=>$ssInfo['id']]);
						$noId = $ssInfo['id']?$ssInfo['id']:$noId;
						$value['no'] = $no;
					}
                    //1正常打卡 2缺卡 3补卡
                    if ($value['no'] == 2 || $value['no'] == 3) {

                        //这个班次缺卡了或已经补卡
                        $rrCondition = " id = {$noId} ";
                        $rrInfo = $this->cardModel->selectOne($rrCondition);

                        $aa['lackCard'] = $value['no'];
                        //1 正常记录  2缺卡记录  3补卡记录
                        $aa['unusualRemark'] = $value['no'] == 3 ? "补卡" : "缺卡";

                        $aa['lackCardData'] = [
                            'isType' => $rrInfo ? 1 : 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => $rrInfo ? $rrInfo['status'] : 4,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => $rrInfo ? date('H:i', $rrInfo['time']) : '',
                        ];
                    } else {
                        //这个班次正常打卡
                        $aa['clockinTime'] = $value['clockinTime'];
                        $aa['timeClockinHi'] = date('H:i', $value['clockinTime']);
                        $aa['abnormalIdentityIs'] = $value['abnormalIdentityIs'];
                        if ($value['abnormalIdentityIs'] != 3) {
                            $aa['abnormalIdentityIsTitle'] = $aa['abnormalIdentityIs'] == 2 ? "未通过" : "已通过";
                        } else {
                            $aa['abnormalIdentityIsTitle'] = "未审核";
                        }
                    }
                    //查看当前班次有无加班通过
                    //判断用户是否加班 $sureTime $date
                    $strtotimeSure = strtotime($value['sureDate'] . " " . $value['sureTime']);
                    $rrCondition = " memberId = {$value['memberId']} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
                    $rrInfo = $this->overtimeModel->selectOne($rrCondition);
                    if ($rrInfo) {
                        $overtimeIs = 2;
                        $overtimeId = $rrInfo['id'];
                    } else {
                        $overtimeIs = 1;
                        $overtimeId = 0;
                    }
                    $aa['overtimeIs'] = $overtimeIs;
                    //1正常记录 2请假记录
                    $aa['overtimeId'] = $overtimeId;
                    $this->attRecordModel->update(" id ={$value['id']}",['overtimeIs'=>$overtimeIs,'overtimeId'=>$overtimeId]);
                    if ($value['overtimeIs'] == 2 || $rrInfo) {
                        //这个班次请假了
                        //                        $ssCondition = " id = {$value['leaveInId']} ";
                        //                        $ssInfo = $this->leaveModel->selectOne($ssCondition);
                        $aa['unusualRemark']  .= ",加班";
                    }
                    //是否外勤
                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $value['sureTime'], $value['sureDate']);
                    $aa['outgoRecordId'] = $isGoRow ? $isGoRow['id'] : 0;
                    if($aa['outgoRecordId']){
                        $this->attRecordModel->update(" id ={$value['id']}",['isGo'=>2]);
                        $value['isGo'] = 2;
                    }
                    if ($value['isGo'] == 2) {
                        //1正常考勤 2外勤打卡
                        $aa['unusualRemark'] .= ",外勤";
                    }
                    //查看当前班次有无请假通过
                    //判断用户是否请假 $sureTime $date
                    $strtotimeSure = strtotime($value['sureDate'] . " " . $value['sureTime']);
                    $rrCondition = " memberId = {$value['memberId']} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
                    $rrInfo = $this->leaveModel->selectOne($rrCondition);
                    if ($rrInfo) {
                        $leaveIn = 2;
                        $leaveInId = $rrInfo['id'];
                    } else {
                        $leaveIn = 1;
                        $leaveInId = 0;
                    }
                    $aa['leaveIn'] = $leaveIn;
                    //1正常记录 2请假记录
                    $aa['leaveInId'] = $leaveInId;
                    $this->attRecordModel->update(" id ={$value['id']}",['leaveIn'=>$leaveIn,'leaveInId'=>$leaveInId]);
                    if ($value['leaveIn'] == 2 || $rrInfo) {
                        //这个班次请假了
                        //                        $ssCondition = " id = {$value['leaveInId']} ";
                        //                        $ssInfo = $this->leaveModel->selectOne($ssCondition);
                        $aa['unusualRemark']  = "请假";
                    }
                    if ($notClockin==2 || ($offDutyOut==2 && $value['clockinType']==2) || $value['leaveIn'] == 2 || $isRest == 2 || $value['isGo'] == 2){
                        $aa['unusualRemark'] = str_replace('早退', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('迟到', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('旷工', '', $aa['unusualRemark']);
                        if(!empty($aa['unusualRemark'])){
                            $aa['unusualRemark'] = implode(',',array_filter(explode(',',$aa['unusualRemark'])));
                        }
                        //强制修改为不早退 迟到 旷工
                        $this->attRecordModel->update(" id ={$value['id']}",['abnormal'=>0,'absenteeism'=>0]);
                    }

                    $ss = 0;
                    if ($value['clockinNum'] == 1 && $value['clockinType'] == 1) {
                        $ss = 0;
                    } elseif ($value['clockinNum'] == 1 && $value['clockinType'] == 2) {
                        $ss = 1;
                    } elseif ($value['clockinNum'] == 2 && $value['clockinType'] == 1) {
                        $ss = 2;
                    } elseif ($value['clockinNum'] == 2 && $value['clockinType'] == 2) {
                        $ss = 3;
                    } elseif ($value['clockinNum'] == 3 && $value['clockinType'] == 1) {
                        $ss = 4;
                    } elseif ($value['clockinNum'] == 3 && $value['clockinType'] == 2) {
                        $ss = 5;
                    }
                    $isData[$ss] = $aa;
                }

                $yData = [
                    'time' => time(),
                    'agId' => $groupmemberrecordInfo['agId'],
                    'notClockin' => $notClockin,
                    'offDutyOut' => $offDutyOut,
                    'agName' => $groupmemberrecordInfo['agName'],
                    'memberId' => $userId,
                    'sure' => 1,
                    'no' => 1,
                    'noId' => 0,
                    'lackCard' => 1,
                    //1 正常记录  2缺卡记录  3补卡记录
                    'leaveIn' => 1,
                    //1正常记录 2请假记录
                    'leaveInId' => 0,
                    //请假ID 默认为0
                    'overtimeIs' => 1,
                    'timeClockinHi' => '',
                    //1正常记录 2请假记录
                    'overtimeId' => 0,
                    //请假ID 默认为0
                    'isGo' => 1,
                    //请假ID 默认为0
                    'faceStatus' => $faceStatus?$faceStatus:2,
                    //是否开启人脸 1开启 2不开启
                    'unusualRemark' => '正常',
                    //异常记录
                    'lackCardData' => [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                        //1审核中 2审核通过 3未通过 4撤销
                        'doTime' => 0,
                    ],
                ];
                $insertRecordData = ['memberId' => $userId, 'agName' => $groupmemberrecordInfo, 'notClockin' => $notClockin, 'offDutyOut' => $offDutyOut, 'isRest' => $isRest, 'agId' => $groupmemberrecordInfo['agId']];
                if (isset($isData[0]) && empty($isData[0])) {
                    $one = $yData;
                    $one['clockinType'] = 1;
                    $one['clockinNum'] = 1;
                    $one['sureTime'] = $goDaysNum[0]['startTime'];
                    //添加判断有无添加缺卡记录
                    $recordS = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=1 and no=2");
                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[0]['startTime'], $date);
                    if (!$recordS) {
                        if ($rrInfo) {
                            $one['leaveIn'] = 2;
                            $one['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['sureTime'] = $goDaysNum[0]['startTime'];
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['clockinNum'] = 1;
                        $insertRecordData['clockinType'] = 1;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $one;
                    $yesData['Id'] = $recordS ? $recordS['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    //1 正常记录  2缺卡记录 3补卡记录  4请假记录
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[0] = $yesData;
                }
                if (isset($isData[1]) && empty($isData[1])) {
                    $two = $yData;
                    $two['clockinType'] = 2;
                    $two['clockinNum'] = 1;
                    $two['sureTime'] = $goDaysNum[0]['endTime'];
                    $oneRcord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=2 and no=2");
                    //添加判断有无添加缺卡记录

                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[0]['endTime'], $date);
                    if (!$oneRcord) {
                        if ($rrInfo) {
                            $two['leaveIn'] = 2;
                            $two['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['sureTime'] = $goDaysNum[0]['endTime'];
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['clockinNum'] = 1;
                        $insertRecordData['clockinType'] = 2;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $two;
                    $yesData['Id'] = $oneRcord ? $oneRcord['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    //1 正常记录  2缺卡记录  3补卡记录
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[1] = $yesData;
                }
                if (isset($isData[2]) && empty($isData[2])) {
                    $three = $yData;
                    $three['clockinType'] = 1;
                    //第二次上班卡
                    $three['clockinNum'] = 2;
                    $three['sureTime'] = $goDaysNum[1]['startTime'];
                    $twoRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=1 and no=2");
                    //添加判断有无添加缺卡记录

                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[1]['startTime'], $date);
                    if (!$twoRecord) {
                        if ($rrInfo) {
                            $three['leaveIn'] = 2;
                            $three['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['sureTime'] = $goDaysNum[1]['startTime'];
                        $insertRecordData['clockinNum'] = 2;
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['clockinType'] = 1;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $three;
                    $yesData['Id'] = $twoRecord ? $twoRecord['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[2] = $yesData;
                }
                if (isset($isData[3]) && empty($isData[3])) {
                    $four = $yData;
                    $four['clockinType'] = 2;
                    $four['clockinNum'] = 2;
                    $four['sureTime'] = $goDaysNum[1]['endTime'];
                    //判断第二次上班卡有无打卡
                    $twoRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=2 and no=2");
                    //添加判断有无添加缺卡记录
                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[1]['endTime'], $date);
                    if (!$twoRecord) {
                        if ($rrInfo) {
                            $four['leaveIn'] = 2;
                            $four['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['sureTime'] = $goDaysNum[1]['endTime'];
                        $insertRecordData['clockinNum'] = 2;
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['clockinType'] = 2;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $four;
                    $yesData['Id'] = $twoRecord ? $twoRecord['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[3] = $yesData;
                }
                if (isset($isData[4]) && empty($isData[4])) {
                    $five = $yData;
                    $five['clockinType'] = 1;
                    $five['clockinNum'] = 3;
                    $five['sureTime'] = $goDaysNum[2]['startTime'];
					$fiveRecordS = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=3 and clockinType=1 and no=2");
                    //添加判断有无添加缺卡记录
                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[2]['startTime'], $date);
                    if (!$fiveRecordS ) {
                        if ($rrInfo) {
                            $five['leaveIn'] = 2;
                            $five['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['sureTime'] = $goDaysNum[2]['startTime'];
                        $insertRecordData['clockinNum'] = 3;
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['clockinType'] = 1;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $five;
                    $yesData['Id'] = $fiveRecordS ? $fiveRecordS['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[4] = $yesData;
                }
                if (isset($isData[5]) && empty($isData[5])) {
                    $six = $yData;
                    $six['clockinType'] = 2;
                    $six['clockinNum'] = 3;
                    $six['sureTime'] = $goDaysNum[2]['endTime'];
					$sixRecordS = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=3 and clockinType=2 and no=2");
                    //添加判断有无添加缺卡记录
                    //查看当前班次有无请假通过
                    $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[2]['startTime'], $date);
                    if (!$sixRecordS) {
                        if ($rrInfo) {
                            $six['leaveIn'] = 2;
                            $six['leaveInId'] = $rrInfo['id'];
                            $insertRecordData['leaveIn'] = 2;
                            $insertRecordData['leaveInId'] = $rrInfo['id'];
                        }
                        $insertRecordData['clockinNum'] = 3;
                        $insertRecordData['sureTime'] = $goDaysNum[2]['endTime'];
                        $insertRecordData['clockinType'] = 2;
                        $insertRecordData['sureDate'] = $date;
                        $insertRecordData['no'] = 2;
                        $id = $this->attRecordModel->insert($insertRecordData);
                    }
                    $yesData = $six;
                    $yesData['Id'] = $sixRecordS ? $sixRecordS['id'] : $id;
                    $yesData['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $yesData['lackCard'] = 2;
                    $yesData['unusualRemark'] = "缺卡";
                    $yesData['lackCardData'] = [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                    ];
                    if ($rrInfo) {
                        $yesData['unusualRemark'] = "请假";
                    }
                    $isData[5] = $yesData;
                }
                if (!$isHolidaysAndFestivals) {
                    $return['status'] = 2;
                } else {
                    $return['status'] = 1;
                }
                $return['data'] = $isData;
            }
        } elseif ($undate >= $startTime && $undate <= $endTime) {
            //选择日期为当天
            if (empty($groupuserRow)) {
                //该用户处于无需考勤人员组中
                $return['code'] = 400;
                $return['status'] = 4;
                $return['msg'] = "该用户没有考勤组,不需要打卡！";
            } else {
                $agId = $groupuserRow[0]['agId'];
                //考勤组ID
                $agName = $groupuserRow[0]['agName'];
                //考勤组名称
                $setholidaysId = $groupuserRow[0]['setholidaysId'];
                //考勤组名称
                $goDaysNum = unserialize($groupuserRow[0]['goDaysNum']);
                //一天上班次数设置及其时间
                $offDutyOut = $groupuserRow[0]['offDutyOut'];
                //下班是否需要打卡 1打卡 2不打卡 （开启后，下班不打卡也不会记作异常）
                $coordinate = unserialize($groupuserRow[0]['coordinate']);
                //签到点坐标 多维数组序列化保存
                $faceStatus = $groupuserRow[0]['faceStatus'];
                //是否开启人脸 1开启 2不开启
                $trackLocation = $groupuserRow[0]['trackLocation'];
                //1 不开启轨迹定位 2 开启轨迹定位
                $unusualStatus = unserialize($groupuserRow[0]['unusualStatus']);
                //异常定义 序列号数组
                //获取日期是否是休息日 bool false是节假日  true不是节假日
                $setholidaysRow = $this->setholidaysModel->selectOne(" id = {$setholidaysId} ", 'workday,specialWorkday,holiday');
                //查询节假日定义
                if ($setholidaysRow) {
                    $workdayKey = unserialize($setholidaysRow['workday']);
                    $specialWorkdayKey = $this->arrayDateToKey(unserialize($setholidaysRow['specialWorkday']));
                    $holidayKey = $this->arrayDateToKey(unserialize($setholidaysRow['holiday']));
                } else {
                    $workdayKey = [];
                    $specialWorkdayKey = [];
                    $holidayKey = [];
                }

                $isHolidaysAndFestivals = $globals->getDateIsHolidaysAndFestivals($date, $workdayKey, $holidayKey, $specialWorkdayKey);
                $isRest = 1;
                //1不是休息日 2休息日
                if (!$isHolidaysAndFestivals) {
                    $return['status'] = 2;
                    //是休息日 2018 07 18 统计休息日打卡
                    $isRest = 2;
                }
                foreach ($coordinate as &$as) {
                    //                    $as['faceStatus'] = $faceStatus;
                    $as['isGo'] = 1;
                }
                //获取公共考勤地点

                $apppublicareaList = $this->apppublicarea->select(" platformId={$platformId} ", 'title,deviation,coordinate')->items;
                foreach ($apppublicareaList as &$a) {
                    $a['coordinate'] = unserialize($a['coordinate']);
                    $a['isGo'] = 3;
                    //1正常 2外勤 3公共
                }
                $coordinate = array_merge($coordinate, $apppublicareaList);
                $num = $goDaysNum ? count($goDaysNum) : 1;
                $isData = [[], [], [], [], [], []];
                switch ($num) {
                    case 1:
                        unset($isData[5]);
                        unset($isData[4]);
                        unset($isData[3]);
                        unset($isData[2]);
                        break;
                    case 2:
                        unset($isData[5]);
                        unset($isData[4]);
                        break;
                }
                //判断人员是否是无需打卡人员
                $attendancegroupuser = $this->groupUserModel->selectOne(" agId={$agId} and memberId={$userId} and type=2 and status=0");
                $notClockin = 1;
                //1需要打卡人员打卡  2无需打卡人员打卡
                if ($attendancegroupuser) {
                    $notClockin = 2;
                }
                //获取当前时间之前的打卡记录
                foreach ($recordInfos as $key => $value) {
                    $aa['Id'] = $value['id'];
                    //
                    $aa['lackCard'] = 1;
                    //1 正常记录  2 补卡记录 3缺卡记录 4请假记录
                    $noId = $value['noId'];
                    $aa['notClockin'] = $notClockin;
                    $aa['noId'] = $noId;
                    $aa['offDutyOut'] = $offDutyOut;
                    $aa['isRest'] = $isRest;
                    $aa['lackCardData'] = [];
                    #基础 start
                    $aa['sure'] = 1;
                    //1有记录 2没有记录
                    $aa['memberId'] = $userId;
                    $aa['agId'] = $value['agId'];
                    $aa['faceStatus'] = $value['faceStatus'];
                    $aa['leaveIn'] = $value['leaveIn'];
                    //1正常记录 2请假记录
                    $aa['leaveInId'] = $value['leaveInId'];
                    //请假ID 默认为0
                    $aa['clockinType'] = $value['clockinType'];
                    $aa['abnormalCoordinateIs'] = $value['abnormalCoordinateIs'];
                    $aa['isGo'] = $value['isGo'];
                    $aa['no'] = $value['no'];
                    $aa['clockinNum'] = $value['clockinNum'];
                    $aa['remark'] = $value['remark'] ? $value['remark'] : '';
                    $aa['abnormalIdentityIs'] = $value['abnormalIdentityIs'];
                    if ($value['abnormalIdentityIs'] != 3) {
                        $aa['abnormalIdentityIsTitle'] = $aa['abnormalIdentityIs'] == 2 ? "未通过" : "已通过";
                    } else {
                        $aa['abnormalIdentityIsTitle'] = "未审核";
                    }
                    $aa['unusualRemark'] = empty($value['unusualRemark']) ? "正常" : $value['unusualRemark'];
                    $aa['clockinTime'] = $value['clockinTime'];
                    $aa['photo'] = empty($value['photo']) ? [] : explode(',', $value['photo']);
                    $aa['timeClockinHi'] = $value['clockinTime'] ? date('H:i', $value['clockinTime']) : '';
                    $clockinCoordinate = unserialize($value['clockinCoordinate']);
                    $clockinCoordinate['title'] = $clockinCoordinate ? $globals->format_html($clockinCoordinate['title']) : "";
                    $aa['clockinCoordinate'] = $clockinCoordinate;
                    $aa['clockinCoordinateName'] = $clockinCoordinate ? $clockinCoordinate['title'] : '未知';
                    $aa['title'] = 3;
                    //1 上班打卡 2还未到打卡时间 3已过打卡时间
                    $aa['sureTime'] = $value['sureTime'];
                    //1迟到 2早退
                    $aa['absenteeism'] = $value['absenteeism'];
                    //0没有旷工 1旷工半天  2旷工1天
                    #基础 end
                    if ($value['abnormal'] == 1) {
                        //1 迟到 2 早退
                        $aa['unusualRemark'] = str_replace('正常', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "迟到";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
                    if ($value['abnormal'] == 2) {
                        //1 迟到 2 早退
                        $aa['unusualRemark'] = str_replace('正常', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "早退";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
                    if ($value['absenteeism'] >= 1) {
                        $aa['unusualRemark'] = str_replace('早退', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('迟到', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = "旷工";
                        $aa['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2 没有补卡记录
                            'isDoSure' => 0,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => 0,
                        ];
                    }
					$ssCondition = "agId={$value['agId']} and memberId = {$value['memberId']} and replacementTime='{$value['sureDate']}' and num={$value['clockinNum']} and baseType={$value['clockinType']} ";
					$ssInfo = $this->cardModel->selectOne($ssCondition);
					if ($ssInfo && $value['no']!=1){
						$no = $ssInfo['status']==2?3:2;
						$this->attRecordModel->update(" id ={$value['id']}",['no'=>$no,'noId'=>$ssInfo['id']]);
						$noId = $ssInfo['id']?$ssInfo['id']:$noId;
						$value['no'] = $no;
					}
                    //1正常打卡 2缺卡 3补卡
                    if ($value['no'] == 2 || $value['no'] == 3) {
                        //这个班次缺卡了或已经补卡
                        $rrCondition = " id = {$noId} ";
                        $rrInfo = $this->cardModel->selectOne($rrCondition);
                        $aa['lackCard'] = $value['no'];
                        //1 正常记录  2缺卡记录  3补卡记录
                        $aa['unusualRemark'] = $value['no'] == 3 ? "补卡" : "缺卡";

                        $aa['lackCardData'] = [
                            'isType' => $rrInfo ? 1 : 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => $rrInfo ? $rrInfo['status'] : 4,
                            //1审核中 2审核通过 3未通过 4撤销
                            'doTime' => $rrInfo ? date('H:i', $rrInfo['time']) : '',
                        ];
                    } else {
                        //这个班次正常打卡
                        $aa['clockinTime'] = $value['clockinTime'];
                        $aa['timeClockinHi'] = date('H:i', $value['clockinTime']);
                        $aa['abnormalIdentityIs'] = $value['abnormalIdentityIs'];
                        if ($value['abnormalIdentityIs'] != 3) {
                            $aa['abnormalIdentityIsTitle'] = $aa['abnormalIdentityIs'] == 2 ? "未通过" : "已通过";
                        } else {
                            $aa['abnormalIdentityIsTitle'] = "未审核";
                        }
                    }
                    //查看当前班次有无加班通过
                    //判断用户是否加班 $sureTime $date
                    $strtotimeSure = strtotime($value['sureDate'] . " " . $value['sureTime']);
                    $rrCondition = " memberId = {$value['memberId']} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
                    $rrInfo = $this->overtimeModel->selectOne($rrCondition);
                    if ($rrInfo) {
                        $overtimeIs = 2;
                        $overtimeId = $rrInfo['id'];
                    } else {
                        $overtimeIs = 1;
                        $overtimeId = 0;
                    }
                    $aa['overtimeIs'] = $overtimeIs;
                    //1正常记录 2请假记录
                    $aa['overtimeId'] = $overtimeId;
                    $this->attRecordModel->update(" id ={$value['id']}",['overtimeIs'=>$overtimeIs,'overtimeId'=>$overtimeId]);
                    if ($value['overtimeIs'] == 2 || $rrInfo) {
                        //这个班次请假了
                        //                        $ssCondition = " id = {$value['leaveInId']} ";
                        //                        $ssInfo = $this->leaveModel->selectOne($ssCondition);
                        $aa['unusualRemark']  .= ",加班";
                    }
                    //是否外勤
                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $value['sureTime'], $value['sureDate']);
                    $aa['outgoRecordId'] = $isGoRow ? $isGoRow['id'] : 0;
                    if($aa['outgoRecordId']){
                        $this->attRecordModel->update(" id ={$value['id']}",['isGo'=>2]);
                        $value['isGo'] = 2;
                    }
                    if ($value['isGo'] == 2) {
                        //1正常考勤 2外勤打卡
                        $aa['unusualRemark'] .= ",外勤";
                    }
                    //查看当前班次有无请假通过
                    //判断用户是否请假 $sureTime $date
                    $strtotimeSure = strtotime($value['sureDate'] . " " . $value['sureTime']);
                    $rrCondition = " memberId = {$value['memberId']} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
                    $rrInfo = $this->leaveModel->selectOne($rrCondition);
                    if ($rrInfo) {
                        $leaveIn = 2;
                        $leaveInId = $rrInfo['id'];
                    } else {
                        $leaveIn = 1;
                        $leaveInId = 0;
                    }
                    $aa['leaveIn'] = $leaveIn;
                    //1正常记录 2请假记录
                    $aa['leaveInId'] = $leaveInId;
                    $this->attRecordModel->update(" id ={$value['id']}",['leaveIn'=>$leaveIn,'leaveInId'=>$leaveInId]);
                    if ($value['leaveIn'] == 2 || $rrInfo) {
                        //这个班次请假了
                        //                        $ssCondition = " id = {$value['leaveInId']} ";
                        //                        $ssInfo = $this->leaveModel->selectOne($ssCondition);
                        $aa['unusualRemark']  = "请假";
                    }
                    if ($notClockin==2 || ($offDutyOut==2 && $value['clockinType']==2) || $value['leaveIn'] == 2 || $isRest == 2 || $value['isGo'] == 2){
                        $aa['unusualRemark'] = str_replace('早退', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('迟到', '', $aa['unusualRemark']);
                        $aa['unusualRemark'] = str_replace('旷工', '', $aa['unusualRemark']);
                        if(!empty($aa['unusualRemark'])){
                            $aa['unusualRemark'] = implode(',',array_filter(explode(',',$aa['unusualRemark'])));
                        }
                        //强制修改为不早退 迟到 旷工
                        $this->attRecordModel->update(" id ={$value['id']}",['abnormal'=>0,'absenteeism'=>0]);
                    }

                    $ss = 0;
                    if ($value['clockinNum'] == 1 && $value['clockinType'] == 1) {
                        $ss = 0;
                    } elseif ($value['clockinNum'] == 1 && $value['clockinType'] == 2) {
                        $ss = 1;
                    } elseif ($value['clockinNum'] == 2 && $value['clockinType'] == 1) {
                        $ss = 2;
                    } elseif ($value['clockinNum'] == 2 && $value['clockinType'] == 2) {
                        $ss = 3;
                    } elseif ($value['clockinNum'] == 3 && $value['clockinType'] == 1) {
                        $ss = 4;
                    } elseif ($value['clockinNum'] == 3 && $value['clockinType'] == 2) {
                        $ss = 5;
                    }
                    $isData[$ss] = $aa;
                }
                $yData = [
                    'time' => time(),
                    'coordinate' => $coordinate,
                    'agId' => $agId,
                    'notClockin' => $notClockin,
                    'offDutyOut' => $offDutyOut,
                    'isRest' => $isRest,
                    'agName' => $agName,
                    'memberId' => $userId,
                    'sure' => 1,
                    'no' => 1,
                    'noId' => 0,
                    'isGo' => 1,
                    'lackCard' => 1,
                    //1 正常记录  2缺卡记录  3补卡记录
                    'leaveIn' => 1,
                    //1正常记录 2请假记录
                    'leaveInId' => 0,
                    'overtimeIs' => 1,
                    //1正常记录 2加班记录
                    'overtimeId' => 0,
                    'timeClockinHi' => '',
                    //请假ID 默认为0
                    'unusualRemark' => '正常',
                    //异常记录
                    'lackCardData' => [
                        'isType' => 2,
                        //1 有补卡记录 2没有补卡记录
                        'isDoSure' => 0,
                        //1审核中 2审核通过 3未通过 4撤销
                        'doTime' => 0
                    ],
                    //迟到或者请假的记录
                    'trackLocation' => $trackLocation,
                    'faceStatus' => $faceStatus?$faceStatus:2,
                    //是否开启人脸 1开启 2不开启
                ];
                $insertRecordData = ['memberId' => $userId, 'agName' => $agName, 'notClockin' => $notClockin, 'offDutyOut' => $offDutyOut, 'isRest' => $isRest, 'agId' => $agId];

                //                判断当前时间 是在哪一个打卡段
                if (isset($goDaysNum[0]['startTime'])) {
                    $onegoDaysStartTime = strtotime($date . " " . $goDaysNum[0]['startTime']);
                }
                if (isset($goDaysNum[0]['endTime'])) {
                    $onegoDaysEndTime = strtotime($date . " " . $goDaysNum[0]['endTime']);
                }
                if (isset($goDaysNum[1]['startTime'])) {
                    $twogoDaysStartTime = strtotime($date . " " . $goDaysNum[1]['startTime']);
                }
                if (isset($goDaysNum[1]['endTime'])) {
                    $twogoDaysEndTime = strtotime($date . " " . $goDaysNum[1]['endTime']);
                }
                if (isset($goDaysNum[2]['startTime'])) {
                    $threegoDaysStartTime = strtotime($date . " " . $goDaysNum[2]['startTime']);
                }
                if (isset($goDaysNum[2]['endTime'])) {
                    $threegoDaysEndTime = strtotime($date . " " . $goDaysNum[2]['endTime']);
                }
                $o = true;
                if (isset($isData[0]) && empty($isData[0])) {
                    $one = $yData;
                    $one['clockinType'] = 1;
                    $one['clockinNum'] = 1;
                    $one['sureTime'] = $goDaysNum[0]['startTime'];
                    //还在打卡时段
                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $one['sureTime']);
                    //判断时间是否在第一次上班打卡时间段
                    if ($t < $onegoDaysEndTime) {

                        if ($isGoRow) {
                            $one['isGo'] = 2;
                            // id,address,lng,lat,radius deviation
                            $isGoCoordinate = [
                                'title' => $isGoRow['address'],
                                'deviation' => $isGoRow['deviation'],
                                'isGo' => 2,
                                //1正常 2外勤 3公共
                                'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']],
                            ];
                            $one['coordinate'] = array_merge($one['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $one['isGo'] = 1;
                        //                        }
                        $one['title'] = 1;
                        //1 上班打卡 2还未到打卡时间
                        $isData[0] = $one;
                    } else {
                        //添加判断有无添加缺卡记录
                        $recordS = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=1 and no=2");
                        if (!$recordS) {
                            //查看当前班次有无请假通过
                            $rrInfo = $this->appUserIsLeval($userId, $goDaysNum[0]['startTime'], $date);
                            if ($rrInfo) {
                                $one['leaveIn'] = 2;
                                $one['leaveInId'] = $rrInfo['id'];
                                $insertRecordData['leaveIn'] = 2;
                                $insertRecordData['leaveInId'] = $rrInfo['id'];
                            }
                            $insertRecordData['isGo'] = $isGoRow?2:1;
                            $insertRecordData['sureTime'] = $goDaysNum[0]['startTime'];
                            $insertRecordData['sureDate'] = $date;
                            $insertRecordData['clockinNum'] = 1;
                            $insertRecordData['clockinType'] = 1;
                            $insertRecordData['no'] = 2;
                            $id = $this->attRecordModel->insert($insertRecordData);
                        }
                        $yesData = $one;
                        $yesData['Id'] = $recordS ? $recordS['id'] : $id;
                        $yesData['title'] = 3;
                        //1 上班打卡 2还未到打卡时间 3已过打卡时间
                        $yesData['lackCard'] = 2;
                        //1 正常记录  2缺卡记录 3补卡记录  4请假记录
                        $yesData['unusualRemark'] = "缺卡";
                        $yesData['lackCardData'] = [
                            'isType' => 2,
                            //1 有补卡记录 2没有补卡记录
                            'isDoSure' => 4,
                        ];

                        if (isset($rrInfo) && !empty($rrInfo)) {
                            $yesData['unusualRemark'] = "请假";
                        }
                        $isData[0] = $yesData;
                    }
                }
                if (isset($isData[1]) && empty($isData[1])) {
                    $two = $yData;
                    $two['clockinType'] = 2;
                    $two['clockinNum'] = 1;
                    $two['sureTime'] = $goDaysNum[0]['endTime'];
                    //判断第一次上班卡有无记录
                    $GoOneStart = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=1");
                    //还在打卡时段
                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $two['sureTime']);
                    //判断时间是否在第一次下班打卡时间段
                    if ($GoOneStart && $t >= $onegoDaysStartTime && (isset($twogoDaysStartTime) ? $t < $twogoDaysStartTime : 1)) {

                        if ($isGoRow) {
                            $two['isGo'] = 2;
                            //1正常考勤 2外勤打卡 id,address,lng,lat,radius deviation
                            $isGoCoordinate = ['title' => $isGoRow['address'], 'deviation' => $isGoRow['deviation'], 'isGo' => 2, 'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']]];
                            $two['coordinate'] = array_merge($two['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $two['isGo'] = 1;
                        //                        }
                        $two['title'] = 1;
                        //1 上班打卡 2还未到打卡时间
                        $isData[1] = $two;
                    } else {
                        $oneRcord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=2 and no=2");
                        if ((($t < $onegoDaysEndTime && !$oneRcord) || !GoOneStart  )  &&  (isset($twogoDaysStartTime) ? $t < $twogoDaysStartTime : 1)) {
                            //还未到打卡时间 当前时间小于 第二次打卡上班开始时间
                            $two['title'] = 2;
                            //1 上班打卡 2还未到打卡时间
                            $isData[1] = $two;
                        } else {
                            //查看当前班次有无请假通过
                            $rrleaveInfo = $this->appUserIsLeval($userId, $goDaysNum[0]['endTime'], $date);
                            //添加判断有无添加缺卡记录
                            if (!$oneRcord) {
                                if ($rrleaveInfo) {
                                    $two['leaveIn'] = 2;
                                    $two['leaveInId'] = $rrleaveInfo['id'];
                                    $insertRecordData['leaveIn'] = 2;
                                    $insertRecordData['leaveInId'] = $rrleaveInfo['id'];
                                }
                                $insertRecordData['isGo'] = $isGoRow?2:1;
                                $insertRecordData['sureTime'] = $goDaysNum[0]['endTime'];
                                $insertRecordData['sureDate'] = $date;
                                $insertRecordData['clockinNum'] = 1;
                                $insertRecordData['clockinType'] = 2;
                                $insertRecordData['no'] = 2;
                                $id = $this->attRecordModel->insert($insertRecordData);
                            }
                            $yesData = $two;
                            $yesData['Id'] = $oneRcord ? $oneRcord['id'] : $id;
                            $yesData['title'] = 3;
                            //1 上班打卡 2还未到打卡时间 3已过打卡时间
                            $yesData['lackCard'] = 2;
                            $yesData['unusualRemark'] = "缺卡";
                            $yesData['lackCardData'] = [
                                'isType' => 2,
                                //1 有补卡记录 2没有补卡记录
                                'isDoSure' => 4,
                            ];
                            if (isset($rrleaveInfo) && !empty($rrleaveInfo)) {
                                $yesData['unusualRemark'] = "请假";
                            }
                            $isData[1] = $yesData;
                        }
                    }
                }
                if (isset($isData[2]) && empty($isData[2])) {
                    $three = $yData;
                    $three['clockinType'] = 1;
                    //第二次上班卡
                    $three['clockinNum'] = 2;
                    $three['sureTime'] = $goDaysNum[1]['startTime'];
                    //判断第一次下班卡有无打卡
                    $GoOneDown = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=1 and clockinType=2");

                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $three['sureTime']);
                    //判断时间是否在第二次上班打卡时间段
                    if ($GoOneDown && $t >= $onegoDaysEndTime && $t < $twogoDaysEndTime) {
                        //还在打卡时段
                        if ($isGoRow) {
                            $three['isGo'] = 2;
                            //1正常考勤 2外勤打卡 id,address,lng,lat,radius deviation
                            $isGoCoordinate = ['title' => $isGoRow['address'], 'deviation' => $isGoRow['deviation'], 'isGo' => 2, 'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']]];
                            $three['coordinate'] = array_merge($three['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $three['isGo'] = 1;
                        //                        }
                        $three['title'] = 1;
                        $isData[2] = $three;
                    } else {
                        $twoRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=1 and no=2");
                        if ((($t < $onegoDaysEndTime || $twoRecord) || !$GoOneDown )  && $t < $twogoDaysEndTime) {
                            //还未到打卡时间 选中时间小于 第三次打卡上班开始时间
                            $three['title'] = 2;
                            $isData[2] = $three;
                        } else {
                            //添加判断有无添加缺卡记录
                            if (!$twoRecord) {
                                //查看当前班次有无请假通过
                                //判断用户是否请假 $sureTime $date
                                $twoleaveInfo = $rrleaveInfo = $this->appUserIsLeval($userId, $goDaysNum[1]['startTime'], $date);
                                if ($twoleaveInfo) {
                                    $three['leaveIn'] = 2;
                                    $three['leaveInId'] = $twoleaveInfo['id'];
                                    $insertRecordData['leaveIn'] = 2;
                                    $insertRecordData['leaveInId'] = $twoleaveInfo['id'];
                                }
                                $insertRecordData['isGo'] = $isGoRow?2:1;
                                $insertRecordData['sureTime'] = $goDaysNum[1]['startTime'];
                                $insertRecordData['clockinNum'] = 2;
                                $insertRecordData['sureDate'] = $date;
                                $insertRecordData['clockinType'] = 1;
                                $insertRecordData['no'] = 2;
                                $id = $this->attRecordModel->insert($insertRecordData);
                            }
                            $yesData = $three;
                            $yesData['Id'] = $twoRecord ? $twoRecord['id'] : $id;
                            $yesData['title'] = 3;
                            //1 上班打卡 2还未到打卡时间 3已过打卡时间
                            $yesData['lackCard'] = 2;
                            $yesData['unusualRemark'] = "缺卡";
                            $yesData['lackCardData'] = [
                                'isType' => 2,
                                //1 有补卡记录 2没有补卡记录
                                'isDoSure' => 4,
                            ];
                            if (isset($twoleaveInfo) && !empty($twoleaveInfo)) {
                                $yesData['unusualRemark'] = "请假";
                            }
                            $isData[2] = $yesData;
                        }
                    }
                }
                if (isset($isData[3]) && empty($isData[3])) {
                    $four = $yData;
                    $four['clockinType'] = 2;
                    $four['clockinNum'] = 2;
                    $four['sureTime'] = $goDaysNum[1]['endTime'];
                    //判断第二次上班卡有无打卡
                    $twoGoUp = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=1");

                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $four['sureTime']);
                    //判断时间是否在第二次下班打卡时间段
                    if ($twoGoUp && $t >= $twogoDaysStartTime && (isset($threegoDaysStartTime) ? $t < $threegoDaysStartTime : 1)) {
                        //还在打卡时段

                        if ($isGoRow) {
                            $four['isGo'] = 2;
                            //1正常考勤 2外勤打卡 id,address,lng,lat,radius deviation
                            $isGoCoordinate = ['title' => $isGoRow['address'], 'deviation' => $isGoRow['deviation'], 'isGo' => 2, 'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']]];
                            $four['coordinate'] = array_merge($four['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $four['isGo'] = 1;
                        //                        }
                        $four['title'] = 1;
                        $isData[3] = $four;
                    } else {
                        $twoRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=2 and no=2");
                        if (($t < $twogoDaysStartTime || !$twoRecord )  && (isset($threegoDaysStartTime) ? $t < $threegoDaysStartTime : 1)) {
                            //还未到打卡时间 当前时间小于 第一次打卡下班开始时间
                            $four['title'] = 2;
                            $isData[3] = $four;
                        } else {
                            $twoleaveInfo = $rrleaveInfo = $this->appUserIsLeval($userId, $goDaysNum[1]['endTime'], $date);

                            //添加判断有无添加缺卡记录
                            if (!$twoRecord) {
                                if ($twoleaveInfo) {
                                    $four['leaveIn'] = 2;
                                    $four['leaveInId'] = $twoleaveInfo['id'];
                                    $insertRecordData['leaveIn'] = 2;
                                    $insertRecordData['leaveInId'] = $twoleaveInfo['id'];
                                }
                                $insertRecordData['isGo'] = $isGoRow?2:1;
                                $insertRecordData['sureTime'] = $goDaysNum[1]['endTime'];
                                $insertRecordData['clockinNum'] = 2;
                                $insertRecordData['sureDate'] = $date;
                                $insertRecordData['clockinType'] = 2;
                                $insertRecordData['no'] = 2;
                                $id = $this->attRecordModel->insert($insertRecordData);
                            }
                            $yesData = $four;
                            $yesData['Id'] = $twoRecord ? $twoRecord['id'] : $id;
                            $yesData['title'] = 3;
                            //1 上班打卡 2还未到打卡时间 3已过打卡时间
                            $yesData['lackCard'] = 2;
                            $yesData['unusualRemark'] = "缺卡";
                            $yesData['lackCardData'] = [
                                'isType' => 2,
                                //1 有补卡记录 2没有补卡记录
                                'isDoSure' => 4,
                            ];
                            if (isset($twoleaveInfo) && !empty($twoleaveInfo)) {
                                $yesData['unusualRemark'] = "请假";
                            }
                            $isData[3] = $yesData;
                        }
                    }
                }
                if (isset($isData[4]) && empty($isData[4])) {
                    $five = $yData;
                    $five['clockinType'] = 1;
                    $five['clockinNum'] = 3;
                    $five['sureTime'] = $goDaysNum[2]['startTime'];
                    //判断第二次下班卡有无记录
                    $GoTwoDown = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=2 and clockinType=2");

                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $five['sureTime']);
                    //判断时间是否在第三次上班打卡时间段
                    if ($GoTwoDown && $t >= $twogoDaysEndTime && $t < $threegoDaysEndTime) {
                        //还在打卡时段
                        if ($isGoRow) {
                            $five['isGo'] = 2;
                            //1正常考勤 2外勤打卡 id,address,lng,lat,radius deviation
                            $isGoCoordinate = ['title' => $isGoRow['address'], 'deviation' => $isGoRow['deviation'], 'isGo' => 2, 'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']]];
                            $five['coordinate'] = array_merge($five['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $five['isGo'] = 1;
                        //                        }
                        $five['title'] = 1;
                        $isData[4] = $five;
                    } else {
                        $twoRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=3 and clockinType=1 and no=2");

                        if (($t < $twogoDaysEndTime || !$twoRecord )  && $t < $threegoDaysEndTime) {
                            //还未到打卡时间 当前时间小于 第三次打卡上班开始时间
                            $five['title'] = 2;
                            $isData[4] = $five;
                        } else {
                            $twoleaveInfo = $rrleaveInfo = $this->appUserIsLeval($userId, $goDaysNum[2]['startTime'], $date);
                           //添加判断有无添加缺卡记录
                            if (!$twoRecord) {
                                if ($twoleaveInfo) {
                                    $five['leaveIn'] = 2;
                                    $five['leaveInId'] = $twoleaveInfo['id'];
                                    $insertRecordData['leaveIn'] = 2;
                                    $insertRecordData['leaveInId'] = $twoleaveInfo['id'];
                                }
                                $insertRecordData['isGo'] = $isGoRow?2:1;
                                $insertRecordData['sureTime'] = $goDaysNum[2]['startTime'];
                                $insertRecordData['clockinNum'] = 3;
                                $insertRecordData['sureDate'] = $date;
                                $insertRecordData['clockinType'] = 1;
                                $insertRecordData['no'] = 2;
                                $id = $this->attRecordModel->insert($insertRecordData);
                            }
                            $yesData = $five;
                            $yesData['Id'] = $twoRecord ? $twoRecord['id'] : $id;
                            $yesData['title'] = 3;
                            //1 上班打卡 2还未到打卡时间 3已过打卡时间
                            $yesData['lackCard'] = 2;
                            $yesData['unusualRemark'] = "缺卡";
                            $yesData['lackCardData'] = [
                                'isType' => 2,
                                //1 有补卡记录 2没有补卡记录
                                'isDoSure' => 4,
                            ];
                            if (isset($twoleaveInfo) && !empty($twoleaveInfo)) {
                                $yesData['unusualRemark'] = "请假";
                            }
                            $isData[4] = $yesData;
                        }
                    }
                }
                if (isset($isData[5]) && empty($isData[5])) {
                    $six = $yData;
                    $six['clockinType'] = 2;
                    $six['clockinNum'] = 3;
                    $six['sureTime'] = $goDaysNum[2]['endTime'];
                    //判断第二次下班卡有无记录
                    $GoThreeUp = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=3 and clockinType=1");
                    //判断时间是否在第三次下班打卡时间段

                    $isGoRow = $this->appUserIsOutGo($userId, $platformId, $six['sureTime']);
                    if ($GoThreeUp && $t >= $threegoDaysStartTime) {
                        //还在打卡时段
                        if ($isGoRow) {
                            $six['isGo'] = 2;
                            //1正常考勤 2外勤打卡 id,address,lng,lat,radius deviation
                            $isGoCoordinate = ['title' => $isGoRow['address'], 'deviation' => $isGoRow['deviation'], 'isGo' => 2, 'coordinate' => ['lng' => $isGoRow['lng'], 'lat' => $isGoRow['lat']]];
                            $six['coordinate'] = array_merge($six['coordinate'], [$isGoCoordinate]);
                        }
                        //                        else{
                        //                            $six['isGo'] = 1;
                        //                        }
                        $six['title'] = 1;
                        $isData[5] = $six;
                    } else {
                        if ($t < $threegoDaysEndTime) {
                            //还未到打卡时间 选中时间小于 第三次打卡上班开始时间
                            $six['title'] = 2;
                            $isData[5] = $six;
                        } else {
                            $threeRecord = $this->attRecordModel->selectOne(" memberId= {$userId} and sureDate='{$date}' and clockinNum=3 and clockinType=2 and no=2");
                            $twoleaveInfo = $rrleaveInfo = $this->appUserIsLeval($userId, $goDaysNum[2]['startTime'], $date);

                            //添加判断有无添加缺卡记录
                            if (!$threeRecord) {
                                if ($twoleaveInfo) {
                                    $six['leaveIn'] = 2;
                                    $six['leaveInId'] = $twoleaveInfo['id'];
                                    $insertRecordData['leaveIn'] = 2;
                                    $insertRecordData['leaveInId'] = $twoleaveInfo['id'];
                                }
                                $insertRecordData['isGo'] = $isGoRow?2:1;
                                $insertRecordData['clockinNum'] = 3;
                                $insertRecordData['sureTime'] = $goDaysNum[2]['endTime'];
                                $insertRecordData['clockinType'] = 2;
                                $insertRecordData['sureDate'] = $date;
                                $insertRecordData['no'] = 2;
                                $id = $this->attRecordModel->insert($insertRecordData);
                            }
                            $yesData = $six;
                            $yesData['Id'] = $threeRecord ? $threeRecord['id'] : $id;
                            $yesData['title'] = 3;
                            //1 上班打卡 2还未到打卡时间 3已过打卡时间
                            $yesData['lackCard'] = 2;
                            $yesData['unusualRemark'] = "缺卡";
                            $yesData['lackCardData'] = [
                                'isType' => 2,
                                //1 有补卡记录 2没有补卡记录
                                'isDoSure' => 4,
                            ];
                            if (isset($twoleaveInfo) && !empty($twoleaveInfo)) {
                                $yesData['unusualRemark'] = "请假";
                            }
                            $isData[5] = $yesData;
                        }
                    }
                }
                if (!$isHolidaysAndFestivals) {
                    $return['status'] = 2;
                } else {
                    $return['status'] = 1;
                }
                $return['data'] = $isData;
            }
        } else {
            //选择日期为未来时间
            if (empty($groupuserRow)) {
                //该用户处于无需考勤人员组中
                $return['code'] = 400;
                $return['msg'] = "该用户没有考勤组,不需要打卡！";
            } else {
                $goDaysNum = unserialize($groupuserRow[0]['goDaysNum']);
                $num = $goDaysNum ? count($goDaysNum) : 1;
                $op = [['sureTime' => $goDaysNum[0]['startTime'], 'clockinType' => 1, 'clockinNum' => 1], ['sureTime' => $goDaysNum[0]['endTime'], 'clockinType' => 2, 'clockinNum' => 1], ['sureTime' => $goDaysNum[1]['startTime'], 'clockinType' => 1, 'clockinNum' => 2], ['sureTime' => $goDaysNum[1]['endTime'], 'clockinType' => 2, 'clockinNum' => 2], ['sureTime' => $goDaysNum[2]['startTime'], 'clockinType' => 1, 'clockinNum' => 3], ['sureTime' => $goDaysNum[2]['endTime'], 'clockinType' => 2, 'clockinNum' => 3]];
                switch ($num) {
                    case 1:
                        unset($op[5]);
                        unset($op[4]);
                        unset($op[3]);
                        unset($op[2]);
                        break;
                    case 2:
                        unset($op[5]);
                        unset($op[4]);
                        break;
                }
                $return['status'] = 5;
                $return['msg'] = "未到打卡时间！";
                $return['data'] = $op;
            }
        }
        return $return;
    }
    /**
     * 获取当前考勤是正常考勤还是外勤考勤 有无外出申请 只针对当天的获取
     * @param $memberId
     * @param $platformId
     * @param $sureTime
     * @return array|bool
     */
    private function appUserIsOutGo($memberId, $platformId, $sureTime, $date = '')
    {
        if (!$date) {
            $date = date('Y-m-d', time());
        }
        //把打卡时间点转换日期时间戳
        $time = strtotime($date . " " . $sureTime);
        $appoutgorecordCondition = " startTime<={$time} and endTime>={$time} and memberId={$memberId} and status=2 and platformId={$platformId} ";

        $appoutgorecordRow = $this->outGoModel->selectOne($appoutgorecordCondition, 'id,address,lng,lat,radius deviation');
        if ($appoutgorecordRow) {
            return $appoutgorecordRow;
        }
        return false;
    }
    //用户在某个日期的考勤 缺卡记录添加
    /**
     * @param $recordInfos  @@选择日期的打卡记录
     * @param $clockinNums  @@应打卡次数
     * @param $agId  @@考勤组ID
     * @param $userId  @@用户ID
     */
    public function insertLackCard($recordInfos, $clockinNums, $agId, $userId)
    {

        if ($clockinNums * 2 != count($recordInfos)) {
            //进行缺卡记录查询
            $yy = [1 => "", 2 => "", 3 => ""];
            foreach ($recordInfos as $riK => $riV) {
                $yy[$riV['clockinNum']] .= $riV['clockinType'] . ",";
            }
            switch ($clockinNums) {
                case 1:
                    $yy[1] = explode(',', $yy[1]);
                    if (!in_array(1, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 1]);
                    } elseif (!in_array(2, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 1]);
                    }
                    break;
                case 2:
                    if (!in_array(1, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 1]);
                    } elseif (!in_array(2, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 1]);
                    }
                    if (!in_array(1, $yy[2])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 2]);
                    } elseif (!in_array(2, $yy[2])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 2]);
                    }
                    break;
                case 3:
                    if (!in_array(1, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 1]);
                    } elseif (!in_array(2, $yy[1])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 1]);
                    }
                    if (!in_array(1, $yy[2])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 2]);
                    } elseif (!in_array(2, $yy[2])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 2]);
                    }
                    if (!in_array(1, $yy[3])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 1, 'no' => 2, 'clockinNum' => 3]);
                    } elseif (!in_array(2, $yy[3])) {
                        $this->attRecordModel->insert(['memberId' => $userId, 'agId' => $agId, 'isGo' => 1, 'clockinType' => 2, 'no' => 2, 'clockinNum' => 3]);
                    }
                    break;
            }
        }
    }
    /**
     * 获取日历状态值返回接口
     */
    public function getCalendarStatusList(AttendanceGroupDTO $attendancegroup)
    {
        $data = $attendancegroup->data ? gdl_lib_BaseUtils::getStr($attendancegroup->data) : "";
        #接受参数
        $date = $data['date'] ? $data['date'] : 0;
        $userId = $data['userId'] ? $data['userId'] : 0;
        $result = new ResultDO();
        $result->success = true;
        $result->code = 200;
        if (empty($userId)) {
            $result->message = "请选择用户";
            return $result;
        }
        if (empty($date)) {
            $result->message = "请选择日期";
            return $result;
        }
        //        $result->message = json_encode($data);
        //        return $result;
        try {
            // 1时间错误红色 2身份地点错误黄色 3都正确绿色 4没有打卡记录
            $t = time();
            $startTime = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));
            //获取当天的0点时间戳
            $endTime = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));
            //获取当天的下午12点时间戳
            $date = strtotime($date);
            //获取选择日期时间戳
            if ($date < $startTime) {
                //1 今天 2 过去 3未来
                $istoday = 2;
            } elseif ($date > $endTime) {
                $istoday = 3;
            } else {
                $istoday = 1;
            }

            
            $globalsModel = new service_globals();
            $tNum = date("t", $date);
            //选择日期的天数
            $start_time = strtotime(date('Y-m-01', $date));
            //获取本月第一天时间戳
            $array = array();
            for ($i = 0; $i < $tNum; $i++) {
                //            $array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
                $dayTime = $start_time + $i * 86400+86399;
                $yMd = date('Y-m-d', $dayTime);
                //每隔一天赋值给数组
                //休息日start
                $type = 1;
                //1工作日 2休息日 3未排班
                $groupmemberrecordInfo = $this->groupmemberrecordModel->selectOne("FIND_IN_SET('{$userId}',memberIds) and addTime<={$dayTime} order by id  desc");

			   if (!$groupmemberrecordInfo) {
                    //当天没有班次添加
                    $type = 3;
                } else {
                    $workdayKey = unserialize($groupmemberrecordInfo['workday']);
                    $holidayKey = $this->arrayDateToKey(unserialize($groupmemberrecordInfo['holiday']));
                    $specialWorkdayKey = $this->arrayDateToKey(unserialize($groupmemberrecordInfo['specialWorkday']));
                    $unusualStatus = unserialize($groupmemberrecordInfo['unusualStatus']);
                    //一天上班次数设置及其时间
                    //获取日期是否是休息日 bool false是节假日  true不是节假日
                    $isHolidaysAndFestivals = $globalsModel->getDateIsHolidaysAndFestivals($yMd, $workdayKey, $holidayKey, $specialWorkdayKey);
                    if ($isHolidaysAndFestivals) {
                        $type = 1;
                    } else {
                        $type = 2;
                    }
                }
                //end
                if ($istoday == 3) {
                    //1 今天 2 过去 3未来
                    $array[] = ['status' => 4, 'date' => $yMd, 'type' => $type];
                } else {
                    //select name, sum(if(score>=60,1,0)),sum(if(score<60,1,0)) from score2 group by name
                    //判断选择日期的打卡时间有没有记录
                    // abnormalIdentityIs人脸验证  abnormalCoordinateIs地点验证 missCard 缺卡  supCard 补卡  leave 请假 field 外勤 late 迟到 leavEarly 早退
                    $lists = $this->attRecordModel->selectOne("memberId = {$userId} and sureDate = '{$yMd}'", "sum(if(abnormalIdentityIs=2,1,0)) as abnormalIdentityIs,sum(if(abnormalCoordinateIs=2,1,0)) as abnormalCoordinateIs,sum(if(no=2,1,0)) as missCard,sum(if(no=3,1,0)) as supCard,sum(if(leaveIn=2,1,0)) as `leave`,sum(if(isGo=2,1,0)) as wQ,sum(if(abnormal=1,1,0)) as late,sum(if(abnormal=2,1,0)) as leavEarly");
                    if ($lists['abnormalIdentityIs'] == null || empty($lists['abnormalIdentityIs']) && $lists['abnormalIdentityIs'] != 0) {
                        $array[] = ['status' => 4, 'date' => $yMd, 'type' => $type];
                    } else {
                        if ($lists['abnormalCoordinateIs'] == 0 && $lists['abnormalIdentityIs'] == 0 && $lists['missCard'] == 0 && $lists['late'] == 0 && $lists['leavEarly'] == 0) {
                            $array[] = ['status' => 3, 'date' => $yMd, 'type' => $type];
                        } elseif (($lists['late'] != 0 || $lists['leavEarly'] != 0 || $lists['missCard'] != 0) && $type == 1) {
                            //节假日不作缺卡或者 时间异常
                            //迟到 或者 早退 红色 ，
                            $array[] = ['status' => 1, 'date' => $yMd, 'type' => $type];
                        } else {
                            //身份异常 或者地点异常 但是时间正常 黄色  ($lists['abnormalCoordinateIs']!=0 || $lists['abnormalIdentityIs']!=0))
                            $array[] = ['status' => 2, 'date' => $yMd, 'type' => $type];
                        }
                    }
                    //特殊查询 当前是否全为请假 如果是则为status=3

                    $leaveIns =  $this->attRecordModel->select("isDelete=0 and memberId = {$userId} and sureDate = '{$yMd}' ", "id,leaveIn,no,abnormal,absenteeism");
                    if ($leaveIns){
                        $leaveS = '';
//                        foreach ($leaveIns as $tyLeave){
//                            if ($tyLeave['leaveIn']==1){
//                                $leaveS = 1;
//                                break;
//                            }
//                        }
                        $leaveInss = array_column($leaveIns,'leaveIn');
                        if (count($leaveInss)>=1 && !in_array(1,$leaveInss)){
                            $leaveS = 0;
                        }
                        if ($leaveS===0){
                            $array[] = ['status' => 3, 'date' => $yMd, 'type' => $type];
                        }
                    }
                }
            }
            $result->data = $array;
            $result->message = '成功';
            $result->code = 200;
        } catch (Exception $e) {
            $result->message = '异常';
        }
        return $result;
    }
    //array [2018-02-11?@(春节(补))','2018-02-11?@(清明节(补))','2018-02-11?@(春节(补))']
    private function arrayDateToKey($array)
    {
        if (!is_array($array)) {
            return false;
        }
        $dateArray = [];
        foreach ($array as $key => $value) {
            $dates = explode('?@', $value);
            $dateArray[] = $dates[0];
        }
        return $dateArray;
    }

    //查看当前班次有无请假通过
    private function appUserIsLeval($memberId, $startTime, $date = '')
    {
        if (!$date) {
            $date = date('Y-m-d', time());
        }
        //查看当前班次有无请假通过
        //判断用户是否请假 $sureTime $date
        $strtotimeSure = strtotime($date . " " . $startTime);
        $rrCondition = " memberId = {$memberId} and startTime <= '{$strtotimeSure}' and endTime>='{$strtotimeSure}' and status=2 ";
        $rrInfo = $this->leaveModel->selectOne($rrCondition);
        if ($rrInfo) {
            return $rrInfo;
        }
        return false;
    }

    /**
     * 添加推送消息
     * @param AttendanceGroupDTO $attendancegroup
     * @return ResultDO
     */
    public function pushMessageGroups(AttendanceGroupDTO $attendancegroup){
        $data = $attendancegroup->data ? gdl_lib_BaseUtils::getStr($attendancegroup->data) : "";
        #接受参数
        $title = $data['title'] ? $data['title'] : '';
        $userId = $data['userId'] ? $data['userId'] : 0;
        $msgId = $data['msgId'] ? $data['msgId'] : 0;
        $allType = $data['allType'] ? $data['allType'] : 0;
        $type = $data['type'] ? $data['type'] : 0;
        $recordId = $data['recordId'] ? $data['recordId'] : 0;
        $approvalId = $data['approvalId'] ? $data['approvalId'] : 0;
        $msgInfo = serialize($data);
        $result = new ResultDO();
        $result->success = true;
        $result->code = 200;
        if (empty($userId)) {
            $result->code = 400;
            $result->message = "请选择用户";
            return $result;
        }
        //        $result->message = json_encode($data);
        //        return $result;
        try {

            $msgModel = new model_attendance_messagerecord();
            $insertMsg['title'] = $title;
            $insertMsg['userId'] = $userId;
            $insertMsg['msgId'] = $msgId;
            $insertMsg['allType'] = $allType;
            $insertMsg['type'] = $type;
            $insertMsg['recordId'] = $recordId;
            $insertMsg['approvalId'] = $approvalId;
            $insertMsg['msgInfo'] = $msgInfo;
            $insertMsg['createTime'] = time();
            $array = $msgModel->insert($insertMsg);
//            $result->data = $array;
            $result->message = '成功';
            $result->code = 200;
        } catch (Exception $e) {
            $result->message = '异常';
        }
        return $result;
    }
}